#include "ConstraintModel.h"
#include "AssetModel.h"

namespace Xps
{
	ConstraintModel::ConstraintModel(XpsAssets::AssetManager* am) : QAbstractItemModel()
	{
		this->am = am;
		map = NULL;
	}

	void ConstraintModel::setDataSource(QMap<int, XpsAssets::ConstraintAttribute*>* data)
	{
		//
		this->map = data;
		reset();
	}

	QVariant ConstraintModel::data(const QModelIndex & index, int role ) const
	{
		if (role == Qt::DisplayRole && index.isValid())
		{
			int key = map->keys().at(index.row());

			/*if (index.column() == 0)
				return QString(am->getAttribute(key)->name());
			*/
			if (index.column() == 0)
			{
				if (map->value(key)->compareMode() == XpsAssets::CompareMode::EQUAL)
					return QString("==");
				if (map->value(key)->compareMode() == XpsAssets::CompareMode::GREATER)
					return QString(">");
				if (map->value(key)->compareMode() == XpsAssets::CompareMode::GREATER_EQUAL)
					return QString(">=");
				if (map->value(key)->compareMode() == XpsAssets::CompareMode::LESS)
					return QString("<");
				if (map->value(key)->compareMode() == XpsAssets::CompareMode::LESS_EQUAL)
					return QString("<=");

				return QString("%1").arg(map->value(key)->compareMode());
			}

			if (index.column() == 1)
				return QString("%1").arg(map->value(key)->value());

			return QString("");
		}
		if (role == Qt::UserRole)
		{
			int key = map->keys().at(index.row());

			return key;
		}
		if (role == Qt::EditRole && index.isValid())
		{
			int key = map->keys().at(index.row());

			if (index.column() == 0)
				return QString("%1").arg(map->value(key)->compareMode());

			if (index.column() == 1)
				return QString("%1").arg(map->value(key)->value());
		}
		return QVariant();
	}

	bool ConstraintModel::setData(const QModelIndex & index, const QVariant & value, int role)
	{
		if (role == Qt::EditRole && index.isValid())
		{
			int key = map->keys().at(index.row());
			XpsAssets::ConstraintAttribute* ca = map->value(key);

			if (index.column() == 0)
				ca->setCompareMode((XpsAssets::CompareMode)value.toInt());
			if (index.column() == 1)
				ca->setValue(value.toInt());

			emit dataChanged(index, index);
			
			return true;
		}
		return false;
	}

	void ConstraintModel::addAttribute(int id)
	{
		beginInsertRows(QModelIndex(), map->count(), map->count());
		XpsAssets::ConstraintAttribute* ca = new XpsAssets::ConstraintAttribute();
		map->insert(id, ca);
		endInsertRows();
	}

	void ConstraintModel::remAttribute(int id)
	{
		int pos = map->keys().indexOf(id);
		beginRemoveRows(QModelIndex(), pos, pos);
		map->remove(id);
		endRemoveRows();
	}

	Qt::ItemFlags ConstraintModel::flags(const QModelIndex & index) const  
	{
		return Qt::ItemFlag::ItemIsSelectable | Qt::ItemFlag::ItemIsEnabled | Qt::ItemFlag::ItemIsEditable;
	}

	int ConstraintModel::rowCount(const QModelIndex &parent) const
	{
		if (map == NULL) return 0;

		return map->count();
	}

	int ConstraintModel::columnCount(const QModelIndex &parent) const
	{
		return 2;
	}

	QModelIndex ConstraintModel::parent(const QModelIndex & index) const
	{
		return QModelIndex();
	}

	QModelIndex ConstraintModel::index(int row, int column, const QModelIndex &parent) const
	{
		if (!hasIndex(row, column, parent))
			return QModelIndex();

		return createIndex(row, column);
	}

	void ConstraintModel::clear()
	{
		reset();
	}

	QVariant ConstraintModel::headerData(int section, Qt::Orientation orientation, int role) const
	{
		static QIcon ico_attribute(":/rs/images/attribute_ref.png");
		
		if (orientation == Qt::Orientation::Vertical)
		{
			if (role == Qt::DisplayRole)
			{
				if (section >= map->keys().count()) 
					return QString();

				int key = map->keys().at(section);
			
				if (am->getAttribute(key) != NULL)
				return am->getAttribute(key)->name();

				return QString();
			}
			if (role == Qt::DecorationRole)
				return ico_attribute;
		}

		return QVariant();
	}
}